package vis;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import logic.PlayfairCipher;
import logic.PlayfairKey;

public class CipherPanel extends JPanel implements KeyListener, ActionListener {
	private static final long serialVersionUID = -9089960991073766639L;

	private KeyPanel keyPanel;
	private TextCheckPanel textBox;
	private JTextField openText = null, keyField = null;
	private PlayfairCipher cipher;
	private JButton showSol;
	private JFrame solFrame = null;
	
	public CipherPanel(PlayfairCipher cipher, boolean decipher, boolean freeform)
	{
		super();
		this.cipher = cipher;
		BoxLayout lm = new BoxLayout(this, BoxLayout.Y_AXIS);
		
		setLayout(lm);
		
		keyPanel = new KeyPanel(cipher, true);
		
		if(!decipher)
			textBox = new TextCheckPanel(new CipherChecker(cipher), "Tekst za šifrovanje", "Šifrat");
		else
			textBox = new TextCheckPanel(new DecipherChecker(cipher), "Šifrat", "Otvoren tekst");
		
		if(freeform)
		{
			JPanel pan = new JPanel();
			pan.setLayout(new GridBagLayout());
			
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = 0; gbc.gridy = 0;
			pan.add(new JLabel("  Otvoren tekst  "), gbc);
			
			openText = new JTextField();
			openText.setFont(new Font(Font.MONOSPACED, Font.BOLD, 16));
			openText.addKeyListener(this);
			openText.setPreferredSize(new Dimension(500, 30));
			
			gbc = new GridBagConstraints();
			gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 0.8;
			pan.add(openText, gbc);
			
			gbc = new GridBagConstraints();
			gbc.gridx = 0; gbc.gridy = 1;
			pan.add(new JLabel("  Ključ  "), gbc);
			
			keyField = new JTextField();
			keyField.setFont(new Font(Font.MONOSPACED, Font.BOLD, 16));
			keyField.addKeyListener(this);
			keyField.setPreferredSize(new Dimension(500, 30));
			keyField.setText("playfairexample");
			
			gbc = new GridBagConstraints();
			gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 0.8;
			pan.add(keyField, gbc);
			
			add(pan);
			
			textBox.setText(" ");
		}
		add(keyPanel);
		add(textBox);
		
		showSol = new JButton("Prikaži rješenje");
		showSol.addActionListener(this);
		add(showSol);
	}
	
	public void setText(String text)
	{
		openText.setText(text);
		
		String c = cipher.sanitizeString(openText.getText());
		c = cipher.getLanguage().doMappings(c);
		if(c.equals(""))
			textBox.setText(" ");
		else
			textBox.setText(c);
	}

	@Override
	public void keyPressed(KeyEvent arg0) {}

	@Override
	public void keyReleased(KeyEvent arg0) {
		if(arg0.getSource() == openText)
		{
			String c = cipher.sanitizeString(openText.getText());
			c = cipher.getLanguage().doMappings(c);
			if(c.equals(""))
				textBox.setText(" ");
			else
				textBox.setText(c);
		}
		else if(arg0.getSource() == keyField)
		{
			cipher.setKey(new PlayfairKey(keyField.getText(), cipher.getLanguage()));
			keyPanel.repaint();
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {}

	@Override
	public void actionPerformed(ActionEvent e) {
		String c = cipher.sanitizeString(openText.getText());
		c = cipher.getLanguage().doMappings(c);
		
		if(c.length() > 0)
		{
			if(solFrame != null)
			{
				if(solFrame.isVisible())
				{
					solFrame.setVisible(false);
				}
				solFrame.dispose();
			}
				
			solFrame = new JFrame();
			KeyPanel kp = new KeyPanel(cipher, false);
			solFrame.add(kp);
			solFrame.pack();
			solFrame.setTitle("Rješenje");
			solFrame.setVisible(true);
			cipher.cipherString(c);
			cipher.playback(kp, 1000);
		}
	}
}
